package com.bjgzy.rating.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerRatingHomeComponent;
import com.bjgzy.rating.mvp.contract.RatingHomeContract;
import com.bjgzy.rating.mvp.presenter.RatingHomePresenter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.Subscriber;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-7 15:54:08
 */
@Route(path = RouterHub.RATING_RATINGHOMEACTIVITY)
public class RatingHomeActivity extends BaseActivity<RatingHomePresenter> implements RatingHomeContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    BaseQuickAdapter<RatingHomeData, AutoBaseViewHolder> mAdapter;
    @Inject
    List<RatingHomeData> lists;
    @Inject
    RecyclerView.LayoutManager mManager;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerRatingHomeComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_rating_home");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.rating_activity_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mSwipeLayout.setOnRefreshListener(() -> {
            getDatas(true);
        });
        rvList.setLayoutManager(mManager);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RatingHomeData data = mAdapter.getData().get(position);
            ARouter.getInstance().build(RouterHub.RATING_RATINGDETAILACTIVITY)
                    .withString(Intent.EXTRA_TITLE,data.getName())
                    .withParcelable(Constants.ROUTER_DATA,data)

                    .navigation();
        });
        mAdapter.setOnLoadMoreListener(() -> {
            getDatas(false);
        },rvList);
        getDatas(true);
    }
    private void getDatas(boolean pullToRefresh) {
        mPresenter.getResearchList(pullToRefresh);
    }

   @Override
   public void showLoading() {
       if (mSwipeLayout != null) {
           mSwipeLayout.setRefreshing(true);
       }
   }

   @Override
   public void hideLoading() {
       if (mSwipeLayout != null) {
           mSwipeLayout.setRefreshing(false);
       }
   }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

     @Subscriber(tag = RouterHub.RATING_RATINGHOMEACTIVITY)
     public void onEvents(Message msg) {
         switch (msg.what) {
             case EventBusTags.WORKS_UPLOAD_SUCCESS:
                 getDatas(true);
                 break;
                 default:
         }
     }
}
